package k3_IO.p2_fileStream.demo02;

import java.io.*;

public class CopyFile {

    public static void copyFileByStream() throws Exception {

        //创建图片的输入流，和目标位置的输出流
        FileInputStream fis = new FileInputStream("E://test1.txt");
        FileOutputStream fos = new FileOutputStream("E://a/test2.txt");

        //设置读取文件字节的缓冲数组
        byte[] byteBuffer = new byte[1024*8];
        //初始化读取到缓冲数组中的实际字节数量
        int length = 0;

        //循环读取数据并写入到目标位置
        while((length = fis.read(byteBuffer))!=-1){
            fos.write(byteBuffer,0,length);
            fos.flush();
        }

        fis.close();
        fos.close();
    }


    public static void copyFileByStreamTransfer() throws Exception {
        //创建图片的输入流，和目标位置的输出流
        FileInputStream fis = new FileInputStream("E://test1.txt");
        FileOutputStream fos = new FileOutputStream("E://a/test2.txt");

        System.out.println(fis.transferTo(fos));

        fis.close();
        fos.close();
    }

    public static void copyFileByReader() throws Exception{

        FileReader fr = new FileReader("E://aaa.png");
        FileWriter fw = new FileWriter("E://a/ccc.png");

        char[] charBuffer = new char[10];
        int length = 0;

        while((length=fr.read(charBuffer))!=-1){
            fw.write(charBuffer,0,length);
        }

        fw.close();
        fr.close();
    }

    public static void copyFileByReaderTransfer() throws Exception{
        FileReader fr = new FileReader("E://test3.txt");
        FileWriter fw = new FileWriter("E://a/test5.txt");

        long length = fr.transferTo(fw);

        fw.close();
        fr.close();
    }

    public static void copyFileByArrays() throws Exception{
        try(
            FileInputStream fis = new FileInputStream("E://test6.txt");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            FileOutputStream fos = new FileOutputStream("E://a/test7.txt");

        ){

            byte[] byteBuffer = new byte[1024*8];
            int length = 0;
            //1.从磁盘读取原始数据，写入到JVM的内存中(第一步的目的地)
            while((length = fis.read(byteBuffer))!=-1){
                baos.write(byteBuffer,0,length);
            }

            byte[] allByte = baos.toByteArray();

            //2.从JVM的内存中读取数据，写出到磁盘中
            //与文件输入流一样的，参数表示的是数据所在的位置，此时数据在数组中
            //文件输入流的参数是文件，数组输入流的参数一定是数组
            ByteArrayInputStream bais = new ByteArrayInputStream(allByte);

            while((length=bais.read(byteBuffer))!=-1){
                fos.write(byteBuffer,0,length);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void copyFileByArraysTransfer() throws Exception{
        try(

            FileInputStream fis = new FileInputStream("E://test6.txt");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            FileOutputStream fos = new FileOutputStream("E://a/test8.txt");

        ){
            //1.从磁盘读取原始数据，写入到JVM的内存中(第一步的目的地)
            fis.transferTo(baos);

            //2.从JVM的内存中读取数据，写出到磁盘中
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            bais.transferTo(fos);


        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
//        CopyFile.copyFileByStreamTransfer();
//        CopyFile.copyFileByReaderTransfer();
        CopyFile.copyFileByArraysTransfer();
    }


}
